define([ "jquery","files/main","comm","mustache.min","text!/static/page/files.html","text!/static/page/file_item.html"],  function($,Files,comm,Mustache,fileTpl,fileItemTpl) {
	var upload,space;
	
	require(['upload/upload'],function(Upload){
		upload = new Upload();
	});
	
	require(['space'],function(s){
		space = s;
	});
	
	function F_search(options){
		this._container = null;
		this._title = null;
		this.panel = null;
		this._target = null;
		this._fileTotal = 0;
		this._totalPage = 0;
		this._curPage = 1;
		this._page = 0;
		this.isShow = false;
		this.isLoading = false;
		this.isClickTitle = false;
		this.files = new Files();
		if(options){
			if(options.target){
				this._target = $(options.target);
				this._title = $(options.target).find(".title");
				this.panel = $(options.target).find(".panel");
			}
			if(options.container){
				this._container = $(options.container);
			}
		}
		
	}
	
	
	F_search.prototype.init = function (){
		this.bindUI();
	}
	
	F_search.prototype.bindUI = function (){
		var itself = this;
		this._title.off().on("click",function(e){
			itself.panel.find(".content").val("搜索：文件名");
			itself.isClickTitle = true;
			$(this).hide();
			itself.panel.fadeIn();
			itself.isShow = true;
		});
		this.panel.find(".content").off().on("focus",function(e){
			comm.stopEvent(e);
			var value =  $(this).val();
			if( value == "搜索：文件名" ){
				$(this).val("");
			}

			$(this).animate({"width":360});
		});
		this.panel.find(".close").off().on("click",function(e){
			comm.stopEvent(e);
			var value = itself.panel.find(".content").val();
			if(value != null && value != undefined && value !="搜索：文件名" && value !=""){
				itself.panel.find(".content").val("");
				itself.panel.find(".content").focus();
				return;
			}
			itself.isClickTitle = false;

			itself.panel.find(".content").animate({width:240},"fast",function(){
				$(".toolbox").find(".icons-tool").show();  //所有的icon功能
				itself.panel.fadeOut("fast",function(){
					itself._title.fadeIn("fast");
				});
			});

			itself.isShow = false;
			if(!itself.isLoading){
				itself.panel.trigger("filesSearch.close");
			}
			$("#current-rows-count").show();
			
		});
		this.panel.find(".search_btn").off().on("click",function(e){
			comm.stopEvent(e);
			if(itself.isLoading){
				return false;
			}
			var lastTitle = $("#folderPath").find("span[haslast='true']");
			itself.fd_id = lastTitle.attr("uuid");
			itself.fd_name = lastTitle.attr("title");
			//console.log(itself);
			var value = itself.panel.find(".content").val();
			if(value =="" || value == null || value == undefined || value == "搜索：文件名"){
				return false;
			}
			if($('#search').data('search') == 1){
				return false;
			}
			$('#search').data('search', 1);
			itself.panel.trigger("filesSearch.search");
			itself.files.remove();
			itself.loadData.apply(itself);
			return false;
		});
		
		this.panel.off().on("click",function(e){
			comm.stopEvent(e);
		});
		
		this.panel.find(".content").on("keydown",function(e){
			comm.stopEvent(e);
			var value = itself.panel.find(".content").val();
			if(value =="" || value == null || value == undefined || value == "搜索：文件名"){
//				alert("请输入条件");
				return;
			}
			
			var oEvent = window.event ? e.keyCode : e.which;
			if (oEvent == 13) {
				if($('#search').data('search') == 1){
					return;
				}
				$('#search').data('search', 1);
				itself.panel.trigger("filesSearch.search");
				itself.loadData.apply(itself);
				
			}
		});


		
		var that  = this;
		$(document).on("keydown", function (e) {
			var target = $(e.target);
			var parents = target.parents('.searchWindow');
			if(target.context != document.body){
				if(target.hasClass("searchWindow") == true || parents.length != 0){
					if(that.isClickTitle){
						that.panel.find(".content").focus();
					}
					return;
				}
				return;
			}
			if(that.isClickTitle){
				that.panel.find(".content").focus();
			}
			
		});
		
		
	}

	F_search.prototype.addLoading = function(){
		if($("#wfo").data("detail")==1)
		{
			var style = "style='padding-right:248px;'";
		}
		$("#waterfall").html('<div class="list-loading-box" '+style+'>正在加载...</div>');
	}
	F_search.prototype.removeLoading = function(){
		$("#waterfall .list-loading-box").remove();
	}
	F_search.prototype.loadData = function (){
		$("#waterfall").children("div").remove();
		this.isLoading = true;
		var that = this;
		/*$.get("/static/page/files.html",function(tmpl){

		});*/
		$('#fileList').remove();
		that._container.prepend(fileTpl);
		/*var height = $(window).height() - parseInt($('.wf_header').height()-2);
		var mypageHeight = height - parseInt($('.tool-view').outerHeight(true)) - parseInt($('.main-title').outerHeight(true));
		$("#mypage").height(mypageHeight);*/

		that.files.container.on("fileList.removeItem",function(e){
			upload.pro_stop_animal();
			space.update(e.file_id);
		});

		that.files.container.on("fileList.toTop",function(e){
			upload.pro_stop_animal();
			upload.clearPro();
		});

		if(!that.isShow){
			$('#search').data('search', 0);
			return;
		}

		//$("#main-title").find(".rt").hide();
		//$("#list-title").hide();  //标题栏
		$("#folderPath").hide();
		$("#fileList").find(".search-detail").show();
		$("#current-rows-count").hide();
		//$("#fileList").find(".content").css('top', '0');  //当存在标题栏时会错位
		require(["folder/main","files/main"],function(Folder,Files){
			var folder = new Folder();
			var files = new Files();
			if($("#wfo").data("detail")==0)
			{
				$("#fileInfo").remove();
				$("#main-title .detail").removeClass("active");
				$("#wfo").data("detail",0);
			}
			else
			{
				folder.defautDetail();
			}
			//folder.isShowDetail();

			$(".toolbox").find(".icons-tool").show();  //所有的icon功能
			$("#tool-view").find(".icon_addDir").hide();  //隐藏文件夹功能
			var keyValue = that.panel.find(".content").val();
			var type = comm.getQueryString("type") || 0;
			//console.log("asdfasdfasdf");return;
			that.addLoading();
			$("#search-detail").find(".key").html('"'+keyValue+ '"');
			$("#search-detail").find(".total").html("...");
			$.getJSON("/files/cd",{keyword:keyValue,type:type}, function(ret) {
				folder._bindUI();
				that.removeLoading();
				if(!that.isShow){
					$('#search').data('search', 0);
					return true;
				}

				if(ret.success){
					var jsonData = ret.data;
					that.isLoading = false;
					that._fileTotal = jsonData.total||0;
					/*that._totalPage = jsonData.totalPage;
					 that._page = jsonData.page;*/
					$("#cur_folder_count").html(that._fileTotal);
					if(that._fileTotal == 0){
						var searchDetail = $("#search-detail");
						searchDetail.find(".key").html('"'+keyValue+ '"');
						searchDetail.find(".total").html(that._fileTotal);
						$("#waterfall").append("<div class='no-search'>没有找到与搜索条件匹配的文件</div>");
						//var no_search = $("#waterfall").find(".no-search");
						//var top = ($("#mypage").height()-no_search.height())/2-5;
						//no_search.css({"top":top,"right":($("#wfo").data("detail")==0?0:248)});
						$('#search').data('search', 0);
						return true;

					}

					$("#search-detail").find(".key").html('"'+keyValue+ '"');
					$("#search-detail").find(".total").html(that._fileTotal);
					files.init(true, jsonData);
					if(!that.isShow){
						$('#search').data('search', 0);
						return true;
					}
					var list = $("#waterfall").find(".col-1").find(".name");
					list.each(function(index,elemen){
						comm.highlight( $(elemen).html(),keyValue,$(elemen));
					});
					$("#nav").find(".text_title_hover").removeClass("text_title_hover");
					$("#nav").find("[data-tab]:eq(0)").find(".text_title").addClass(
						"text_title_hover");

					$('#search').data('search', 0);
					return false;
				}
			});
			//return false;


		});

		return false;
	}
	
	F_search.prototype.nextData = function (curPage,call){
		var keyValue = $("#search").find(".content").val();
		$.getJSON("/f_search",{keyword:keyValue,page:curPage},  function(jsonData) {
			if(jsonData.ret == -1){
				
			}

			
		});
		return false;
	}
	
	F_search.prototype.close = function (){
		if(!this.isShow){
			return;
		}
		var itself = this;
		var keyValue = this.panel.find(".content").val();
		if(keyValue == "搜索：文件名" || keyValue == ""){
			/*this._title.show();
			this.panel.hide();
			this.isClickTitle = false;*/
			itself.panel.find(".content").animate({width:240},"fast",function(){
				itself.panel.fadeOut("fast",function(){
					itself._title.fadeIn("fast");
				});
			});
			itself.isClickTitle = false;
		}
		else
		{
			itself.panel.find(".content").animate({width:240},"fast");
		}
	}
	
	F_search.prototype.forceClose = function(){
		if(!this.isShow){
			return;
		}
		this._title.show();
		this.panel.hide();

		this.isShow = false;
		this.isClickTitle = false;
		if(!this.isLoading){
			this.destroy();
			
			this.panel.trigger("filesSearch.close");
		}
		
		
	}
	
	F_search.prototype.destroy = function(){
		$("#fileList").remove();
	}
	
	return F_search;
	
});